package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.entity.qi.QiTatVsItem;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.mapper.qi.QiTatVsItemMapper;
import com.ysd.lis.request.qi.TatItemDto;
import com.ysd.lis.request.qi.TatItemParam;
import com.ysd.lis.service.qi.QiTatVsItemService;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 任务与指标项目对照表 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-06
 */
@Service
public class QiTatVsItemServiceImpl extends ServiceImpl<QiTatVsItemMapper, QiTatVsItem> implements QiTatVsItemService {

    @Autowired
    QiTatVsItemMapper qiTatVsItemMapper;
    @Autowired
    LabItemDictMapper labItemDictMapper;
    @Override
    public Result findLabItemDictListByTat(TatItemParam param) {
        MPJLambdaWrapper<LabItemDict> lambdaQueryWrapper = new MPJLambdaWrapper();
        lambdaQueryWrapper.selectAll(LabItemDict.class);
        lambdaQueryWrapper.selectAs("rptgrp", LabRptGroup::getRptGroupName, LabItemDict::getRptGroupName);
        lambdaQueryWrapper.selectAs("rptgrp", LabRptGroup::getSeq, LabItemDict::getRptSeq);

        lambdaQueryWrapper.leftJoin(LabRptGroup.class, "rptgrp", p->p.eq(LabRptGroup::getRptGroup, LabItemDict::getRptGroup).eq(LabRptGroup::getDelFlag,0) );

        lambdaQueryWrapper.eq(LabItemDict::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getRptGroup())) {
            lambdaQueryWrapper.eq(LabItemDict::getRptGroup, param.getRptGroup());
        }

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabItemDict::getItemNo, param.getSeachValue()).or().like(LabItemDict::getItemNa, param.getSeachValue()).or().like(LabItemDict::getItemEnName, param.getSeachValue()).or().like(LabItemDict::getItemEnCode, param.getSeachValue()).or().like(LabItemDict::getInputCode, param.getSeachValue()));
        }
        StringBuffer existsSql = new StringBuffer("SELECT 1 FROM QI_TAT_VS_ITEM t2 WHERE t2.DEL_FLAG=0 and t.ITEM_NO = t2.ITEM_NO and t.RPT_GROUP = t2.RPT_GROUP ");
        if(ToolsUtils.isNotEmpty(param.getTatTypeCode())){
            existsSql.append(" and t2.TAT_TYPE_CODE = '"+param.getTatTypeCode()+"' ");
        }
        if(ToolsUtils.isNotEmpty(param.getTatItemCode())){
            existsSql.append(" and t2.TAT_ITEM_CODE = '"+param.getTatItemCode()+"' ");
        }
        lambdaQueryWrapper.notExists(existsSql.toString());
        lambdaQueryWrapper.isNotNull(LabItemDict::getRptGroup);
        lambdaQueryWrapper.orderByAsc(LabRptGroup::getSeq).orderByAsc(LabItemDict::getSeq);
        List<LabItemDict> labItemDicts = labItemDictMapper.selectList(lambdaQueryWrapper);
        List<LabItemDict> collect1 = labItemDicts.stream().sorted(Comparator.comparing(LabItemDict::getRptSeq).thenComparing(LabItemDict::getSeq, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());

        return Result.succ(1, "查询成功！", collect1);

    }

    @Override
    public Result findLabItemDictPageByTat(TatItemParam param) {

        Page<LabItemDict> page = new Page<>();
        page.setCurrent(param.getPageIndex());
        page.setSize(param.getPageSize());

        MPJLambdaWrapper<LabItemDict> lambdaQueryWrapper = new MPJLambdaWrapper();
        lambdaQueryWrapper.selectAll(LabItemDict.class);
        lambdaQueryWrapper.selectAs("rptgrp", LabRptGroup::getRptGroupName, LabItemDict::getRptGroupName);
        lambdaQueryWrapper.selectAs("rptgrp", LabRptGroup::getSeq, LabItemDict::getRptSeq);

        lambdaQueryWrapper.leftJoin(LabRptGroup.class, "rptgrp", p->p.eq(LabRptGroup::getRptGroup, LabItemDict::getRptGroup).eq(LabRptGroup::getDelFlag,0) );

        lambdaQueryWrapper.eq(LabItemDict::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getRptGroup())) {
            lambdaQueryWrapper.eq(LabItemDict::getRptGroup, param.getRptGroup());
        }

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabItemDict::getItemNo, param.getSeachValue()).or().like(LabItemDict::getItemNa, param.getSeachValue()).or().like(LabItemDict::getItemEnName, param.getSeachValue()).or().like(LabItemDict::getItemEnCode, param.getSeachValue()).or().like(LabItemDict::getInputCode, param.getSeachValue()));
        }
        StringBuffer existsSql = new StringBuffer("SELECT 1 FROM QI_TAT_VS_ITEM t2 WHERE t2.DEL_FLAG=0 and t.ITEM_NO = t2.ITEM_NO and t.RPT_GROUP = t2.RPT_GROUP ");
        if(ToolsUtils.isNotEmpty(param.getTatTypeCode())){
            existsSql.append(" and t2.TAT_TYPE_CODE = '"+param.getTatTypeCode()+"' ");
        }
        if(ToolsUtils.isNotEmpty(param.getTatItemCode())){
            existsSql.append(" and t2.TAT_ITEM_CODE = '"+param.getTatItemCode()+"' ");
        }
        lambdaQueryWrapper.notExists(existsSql.toString());
        lambdaQueryWrapper.isNotNull(LabItemDict::getRptGroup);
        lambdaQueryWrapper.orderByAsc(LabRptGroup::getSeq).orderByAsc(LabItemDict::getSeq);
        Page<LabItemDict> labItemDicts = labItemDictMapper.selectPage(page,lambdaQueryWrapper);
        labItemDicts.setRecords(labItemDicts.getRecords().stream().sorted(Comparator.comparing(LabItemDict::getRptSeq, Comparator.nullsLast(Integer::compareTo)).thenComparing(LabItemDict::getSeq, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList())) ;

        return Result.succ(1, "查询成功！", labItemDicts);
    }

    @Override
    public Result addTatVsItem(QiTatVsItem qiTatVsItem) {
        int insert = qiTatVsItemMapper.insert(qiTatVsItem);
        if (insert > 0) {
            return Result.succ(1, "添加成功", qiTatVsItem);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result findQiTatVsItemList(TatItemParam param) {
        MPJLambdaWrapper<QiTatVsItem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QiTatVsItem.class);
        queryWrapper.selectAs("pu", LabItemDict::getItemNa, QiTatVsItem::getItemNa);
        queryWrapper.selectAs("sa", LabRptGroup::getRptGroupName, QiTatVsItem::getRptGroupName);
        queryWrapper.leftJoin(LabItemDict.class, "pu", p -> p.eq(LabItemDict::getItemNo, QiTatVsItem::getItemNo).eq(LabItemDict::getRptGroup, QiTatVsItem::getRptGroup).eq(LabItemDict::getDelFlag, 0));
        queryWrapper.leftJoin(LabRptGroup.class, "sa", p -> p.eq(LabRptGroup::getRptGroup, QiTatVsItem::getRptGroup).eq(LabRptGroup::getDelFlag, 0));
        queryWrapper.eq(QiTatVsItem::getDelFlag, 0);
        queryWrapper.eq(QiTatVsItem::getTatTypeCode,param.getTatTypeCode());
        queryWrapper.eq(QiTatVsItem::getTatItemCode,param.getTatItemCode());
        if(ToolsUtils.isNotEmpty(param.getSeachValue())){
            queryWrapper.and(wrapper ->
                    wrapper.like(QiTatVsItem::getItemNo, param.getSeachValue())
                            .or()
                            .like(QiTatVsItem::getRptGroup, param.getSeachValue())
            );
        }
        queryWrapper.orderByAsc(QiTatVsItem::getSeq);
        List<QiTatVsItem> detailList = qiTatVsItemMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result deleteTatVsItem(QiTatVsItem qiTatVsItem) {
        int deleteById = qiTatVsItemMapper.deleteById(qiTatVsItem.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteTatItemList(TatItemDto dto) {
        List<QiTatVsItem> qiTatVsItemList = dto.getQiTatVsItemList();
        List<String> ids = qiTatVsItemList.stream().map(QiTatVsItem::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);

        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result saveOrUpdateTatVsItem(TatItemDto dto) {
        List<QiTatVsItem> qiTatVsItemList = dto.getQiTatVsItemList();
        boolean b = this.updateBatchById(qiTatVsItemList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }


}
